package com.authine.cloudpivot.web.api.service.impl;



import com.authine.cloudpivot.engine.api.model.runtime.WorkItemModel;
import com.authine.cloudpivot.web.api.dao.PROPERTYMapper;
import com.authine.cloudpivot.web.api.service.TransferService;
import com.authine.cloudpivot.web.api.view.runtime.TransferVO;
import com.authine.cloudpivot.web.api.view.runtime.WorkItemVO;
import com.authine.cloudpivot.web.api.vo.PROPERTY;
import com.authine.cloudpivot.web.api.vo.XMQD;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Slf4j
@Service
public class TransferServiceImpl implements TransferService {

    @Autowired
    private PROPERTYMapper propertyMapper;


    /**
     * @Description : 查询需要更换的数据，并将数据转换为TransferVO
     * @param ： projectId
     * @param ： subjectPersonOld
     * @param ： subjectPersonNew
     * @author : Liun
     * @date : 2020/7/13
     */
   @Override
   public  List<TransferVO> selectTransferVO(String projectId,String userId,String transferUserId,String transferDeptId,List<WorkItemModel> workItemVOList){
       String projectIdOld=projectId;
       List<TransferVO> transferVOS=new ArrayList<>();
       //项目立项被关联
       List<PROPERTY> propertyList=propertyMapper.selectProperty();
       List<String> workItemIds=new ArrayList<>();
       String workflowCode="";
       for(PROPERTY property:propertyList){
           String tableName=property.getSchemacode();
           String  code=property.getCode();
           String schemacode="iden7_"+tableName;
           int count= propertyMapper.checkOwner(schemacode);
           if(count==1){
               //业务数据交接
               List<String> transferVOIds=propertyMapper.selectTransferVOId(projectId,schemacode,code,userId);
               transferVOS=createTransferVO(transferVOS, tableName,transferDeptId,transferUserId ,userId, transferVOIds);
               //流程待办数据交接
               workItemIds=  createTransferVOWork(workItemVOList,workflowCode,tableName,
                       schemacode,code,projectId,workItemIds);
               }
       }

       //项目立项去关联或者为项目立项数据
       List<PROPERTY> propertyListOther=propertyMapper.selectPropertyOther();
       XMQD xmqd=propertyMapper.selectXMQD(projectId);
       List<String> transferVOIdList=new ArrayList<>();
       for(PROPERTY property:propertyListOther){
          String  code=property.getCode();
          String  tableName=property.getRelativecode();
          String  relativecode="iden7_"+property.getRelativecode();
          String id="id";
          if(id.equals(code)){
               transferVOIdList=new ArrayList<>();
               transferVOIdList.add(projectIdOld);
           }
           if("subjectName".equals(code)){
               projectId=xmqd.getSubjectname();
               transferVOIdList=propertyMapper.selectTransferVOId(projectId,relativecode,id,userId);
           }

           if("terms".equals(code)){
               projectId=xmqd.getTerms();
               transferVOIdList=propertyMapper.selectTransferVOId(projectId,relativecode,id,userId);
           }

           if("customName".equals(code)){
               projectId=xmqd.getCustomname();
               transferVOIdList=propertyMapper.selectTransferVOId(projectId,relativecode,id,userId);
           }

           transferVOS=createTransferVO(transferVOS, tableName,transferDeptId,transferUserId ,userId, transferVOIdList);
           workItemIds=  createTransferVOWork(workItemVOList,workflowCode,tableName,
                   relativecode,code,projectId,workItemIds);
       }

       //待办流程数据
       if(workItemIds.size()>0){
           TransferVO transferVOWork=new TransferVO();
           transferVOWork.setOriginator("");
           transferVOWork.setTransferAll(false);
           transferVOWork.setTransferUserId(transferUserId);
           transferVOWork.setUserId(userId);
           transferVOWork.setWorkItemIds(workItemIds);
           transferVOS.add(transferVOWork);
       }
       return transferVOS;
   }


   /**
    * @Description : 生成业务交接数据
    * @param ： transferVOS
    * @param ： tableName
    * @param ： transferDeptId
    * @param ： transferUserId
    * @param ： userId
    * @param ： transferVOIds
    * @author : Liun
    * @date : 2020/7/15
    */
   private List<TransferVO> createTransferVO(List<TransferVO> transferVOS, String tableName,String transferDeptId,String transferUserId ,String userId,List<String> transferVOIds){
        if(transferVOIds.size()>0){
            TransferVO transferVO=new TransferVO();
            transferVO.setIsBusiness(true);
            transferVO.setIsSyncInstance(false);
            transferVO.setSchemaCode(tableName);
            transferVO.setSequenceStatus("");
            transferVO.setTransferAll(false);
            transferVO.setTransferDeptId(transferDeptId);
            transferVO.setTransferUserId(transferUserId);
            transferVO.setUserId(userId);
            transferVO.setWorkItemIds(transferVOIds);
            transferVOS.add(transferVO);
        }
        return transferVOS;
    }


    private List<String> createTransferVOWork(List<WorkItemModel> workItemVOList,String workflowCode,String tableName,
                                              String schemacode,String code,String projectId,List<String> workItemIds){
        for(WorkItemModel workItemVO:workItemVOList){
            log.info("workItemVO--------->"+workItemVO.getWorkflowCode());
            workflowCode=workItemVO.getWorkflowCode();
            List<String>  relativeCodes=propertyMapper.selectTableName(workflowCode);
            for(String relativeCode:relativeCodes){
                if(tableName.equals(relativeCode)){
                    int countNum= propertyMapper.checkProject(schemacode,workItemVO.getInstanceId(),"b."+code,projectId);
                    if(countNum>0){
                        workItemIds.add(workItemVO.getId());
                    }
                    break;
                }
            }
        }
        return workItemIds;
    }


}



